package com.lotusfall.controller;

import com.github.pagehelper.PageInfo;
import com.lotusfall.pojo.Ad;
import com.lotusfall.pojo.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lotusfall.service.AdService;
import com.lotusfall.vo.WebResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/home")
@CrossOrigin
public class AdController {
    @Autowired
    private AdService adService;

    @RequestMapping("/swiper")
    @GetMapping
    public WebResult queryIndexAd(){
        PageInfo<Ad> adPageInfo = this.adService.queryAdList(1, 1, 3);
        List<Ad> list = adPageInfo.getList();
        List<Object> result = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",i);
            map.put("imgSrc",list.get(i).getUrl());
            map.put("alt",null);
            result.add(map);
        }

        return WebResult.ok(list);
    }

    @RequestMapping("/comments")
    @GetMapping
    public WebResult queryComments(){
        PageInfo<Comment> commentPageInfo = this.adService.queryCommentList(1, 3);
        List<Comment> list = commentPageInfo.getList();
        List<Object> result = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",i);
            map.put("imgSrc",list.get(i).getUrl());
            map.put("desc",list.get(i).getDesc());
            result.add(map);
        }
        return WebResult.ok(list);
    }

}
